<?php
namespace app\api\controller;

use app\common\model as M;

/**
 * 用户消息列表
 */
class Message extends \app\api\ApiAuth
{
    public $model = '\app\common\model\Message';

    // 消息标记已读
    public function mark_read()
    {
        $baseFilter = sprintf("uid=%d AND read_time=0" , $this->uid);

        $id = (int)$this->request->post('id');
        if ( $id > 0 ) {
            $baseFilter .= sprintf(" AND id=%d" , $id);
        }
        $res = $this->model::where($baseFilter)->update(['read_time' => time()]);
        if ( $res !== false ) return $this->returnSuccess();

        return $this->returnFail('操作失败');
    }

    // 消息清除
    public function mark_clear()
    {
        $baseFilter = sprintf("uid=%d" , $this->uid);
        $id = (int)$this->request->post('id');
        if ( $id > 0 ) {
            $baseFilter .= sprintf(" AND id=%d" , $id);
        }

        $res = $this->model::where($baseFilter)->delete();
        if ( $res !== false ) return $this->returnSuccess();

        return $this->returnFail('清除失败');
    }
}//Class End